package com.thwcompany.rabbitMQ.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2022/10/24  15:41
 * @Description:
 */
@SuppressWarnings("all")
@Component
@RabbitListener(queues = "direct-pmc-queue")
public class PmcListener1 {
    //发布-订阅模式，这里配置消费者1号监听消息队列direct-pmc-queue
    //@RabbitHandler
    //public void displayMail(String msg , Channel channel, Message message) throws Exception {
    //    System.out.println("direct消费者1号，从direct-pmc-queue消息队列监听到的消息是："+msg);
    //    //消费者1号进行手工确认签收消息
    //    long deliveryTag = message.getMessageProperties().getDeliveryTag();
    //    System.out.println("消费者1号进行手工确认签收消息的消息唯一标识deliveryTag："+deliveryTag);
    //    //拒绝消息
    //    channel.basicReject(deliveryTag,false);
    //}
    @RabbitHandler
    public void receiveMessage(String msg ,Channel channel, Message message) throws Exception {
        // 获取消息体（这里是字符串）
        String msg1 = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("direct消费者1号，从direct-pmc-queue消息队列监听到的消息是："+msg);
        System.out.println("direct消费者1号，从direct-pmc-queue消息队列监听到的消息是："+msg1);

        Integer priority=message.getMessageProperties().getPriority();
        System.out.println("direct消费者1号接收的消息的priority："+priority);
        String  expiration=message.getMessageProperties().getExpiration();
        System.out.println("direct消费者1号接收的消息的expiration："+expiration);

        // 获取消息ID
        String messageId = message.getMessageProperties().getMessageId();
        System.out.println("direct消费者1号进行手工确认签收消息的消息唯一标识messageId："+messageId);

        //消费者1号进行手工确认签收消息
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("direct消费者1号进行手工确认签收消息的消息唯一标识deliveryTag："+deliveryTag);
        //拒绝消息
        channel.basicReject(deliveryTag,false);
    }
}
